// pages/addresspages/addressList/addressList.js
var Ajax = require('../../../utils/Ajax.js')
var utils = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    statusType: '',//数据是否为空 value:empty
    moreType: 'noMore',
    nameOrPhone: '',
    title: '暂无收货地址',
    statuserro: false,//数据查询错误并且数组为空 true为错误 false正常
    isCheck: false,
  },
  oninput(e) { //获取输入的值
    var {
      name
    } = e.currentTarget.dataset;
    var { value } = e.detail;
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“/\"'。\\，、_？\\s+]")
    this.setData({
      [name]: value.replace(pattern, "")
    })
  },
  oninpfocus(e) {//输入框获取焦点
    var inputname = e.target.dataset.name;
    this.setData({ atv: inputname })
  },
  oninpblur(e) {//输入框失去焦点
    var inputname = e.target.dataset.name
    this.setData({ atv: '' })
  },
  delText(e) { //清空input
    this.setData({
      ['' + e.detail]: ''
    }, () => {
      this.queryAddress();
    })
  },
  queryAddress() {
    wx.showNavigationBarLoading();
    var { nameOrPhone, title } = this.data;
    Ajax.post('getReceiptAddressList',{
      uid: '67301',
      nameOrPhone
    }).then((d) => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (d.status == 200) {
        var data = d.data, moreType = "showText";
        if (data) {
          if (nameOrPhone && d.data.length == 0) {
            title = `未找到"${nameOrPhone}"相关地址,下拉刷新显示全部地址列表`
          } else {
            title = '暂无收货地址'
          }
          // data.length < numPerPage ? moreType = "noMore" : moreType
          this.setData({ dataList: data, moreType: "none", title })
        } else {
          this.setData({ moreType: "noMore" });
        }
      } else {
        wx.hideLoading();
        utils.showLoading(d.data)
        this.setData({ moreType: "none" })
      }
    }).catch(() => {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        moreType: "none"
      })
    })
  },
  searchSubmit() {
    this.queryAddress();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var isCheck = options.isCheck == "true" || options.isCheck == true;
    var Invoiceaddr = options.Invoiceaddr
    this.setData({
      isCheck: isCheck,
      InvoiceType: Invoiceaddr ? Invoiceaddr : '',
      parentData: options,
      model: app.globalData.model,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.queryAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var { dataList } = this.data;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if (!dataList.length) {//防止地址删除后显示遗留
      prevPage.setData({
        defaultAddressInfo: {},
        defaultInvoiceAddressInfo: {},
        backpage: false
      })
    } else {//防止在管理地址里面编辑影响显示
      var addressid = prevPage.data&&prevPage.data.defaultAddressInfo&&prevPage.data.defaultAddressInfo.id
      dataList.forEach((item) => {
        if (item.id == addressid) {
          var { address, provinceName, cityName, areaName, } = item;
          item.address = provinceName + ' ' + cityName + ' ' + areaName + ' ' + address;
          prevPage.setData({ defaultAddressInfo: item})
        }
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      nameOrPhone: ''
    }, () => {
      this.queryAddress();
    })
  },
  onEditorbtn(e) {//用户编辑地址
    var item = e.detail.items;
    utils.toPage(`/pages/addresspages/addressEditor/addressEditor?` + utils.queryconcat(item))
  },
  onAddrbtn(e) {//用户新建地址
    var item = e.detail.items;
    var { dataList } = this.data;
    var isdefault = 0;
    if (!dataList.length) {
      isdefault = 1;
    }
    utils.toPage(`/pages/addresspages/addressAdd/addressAdd?isdefault=${isdefault}`)
  },
  onSelectaddress(e) {//用户选择地址
    var that = this
    var item = e.detail.items;
    var pages = getCurrentPages();
    var { address, provinceName, cityName, areaName, } = item;
    item.address = provinceName + ' ' + cityName + ' ' + areaName + ' ' + address;
    if (this.data.parentData.checkAddress == "1") {
      //  获取页面栈
      var prevPage = pages[pages.length - 2];    // 上一个页面
      prevPage.setData({
        defaultAddressInfo: item,
        backpage: item.selected==0 ? false:true//选择的是默认地址
      })
      wx.navigateBack({
        delta: 1
      })
    } else if (this.data.InvoiceType == 1) {
      var prevPage = pages[pages.length - 2];    // 上一个页面
      prevPage.setData({
        defaultInvoiceAddressInfo: item,
        backpage: true
      })
      wx.navigateBack({
        delta: 1
      })
    }

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})